<template>
  <div>
    <el-input v-model="currentValue!.content" type="textarea" :rows="3"></el-input>
    <div class="flex justify-between mt-2">
      <div class="flex gap-2">
        <EmojiPopover @select="handleInsert"></EmojiPopover>
        <div class="cursor-pointer" style="width: 200px">
          <el-upload
            ref="upload"
            action="#"
            :limit="1"
            :on-exceed="handleExceed"
            :http-request="handleUpload"
          >
            <Icon :class="['action']" icon="jpg" :size="18" colorful />
            <span class="ml-1">图片</span>
          </el-upload>
        </div>
      </div>
      <slot></slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import EmojiPopover from '@/components/emojiPopover/index.vue'
import { genFileId } from 'element-plus'
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'

const currentValue = defineModel<{ content: string; images: string[] }>()

const handleInsert = (emoji: string) => {
  currentValue.value!.content = currentValue.value!.content + emoji
}

const upload = ref<UploadInstance>()
const handleExceed: UploadProps['onExceed'] = (files) => {
  upload.value!.clearFiles()
  const file = files[0] as UploadRawFile
  file.uid = genFileId()
  upload.value!.handleStart(file)
}

const handleUpload = async (options: any) => {
  const data = {
    file: options.file,
    filename: options.file.name,
    onProgress: options.onProgress,
  }
  const result = await service.common.upload(data)
  currentValue.value!.images = []
  currentValue.value!.images.push(result)
}
</script>
